import classNames from 'classnames';
import { useEffect, useRef } from 'react';
import styles from './index.module.scss';

interface ITextAreaProps
  extends React.DetailedHTMLProps<
    React.TextareaHTMLAttributes<HTMLTextAreaElement>,
    HTMLTextAreaElement
  > {
  value: string;
}

export default function Textarea({
  value = '',
  onChange,
  maxLength = 100,
  className,
  ...restProps
}: ITextAreaProps) {
  const textRef = useRef<HTMLTextAreaElement>(null);
  useEffect(() => {
    textRef.current?.focus();
    textRef.current?.setSelectionRange(-1, -1);
  }, []);

  return (
    <div className={styles.root}>
      <textarea
        ref={textRef}
        className={classNames('textarea', className)}
        onChange={onChange}
        value={value}
        {...restProps}
      />
      <div className="count">
        {value.length}/{maxLength}
      </div>
    </div>
  );
}
